/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

@title: buildBurger
@description: A game where you build burgers to match the burger you're shown.
@author: Kevin
@tags: []
@addedOn: 2024-11-26
*/
const cursor = "c"
const bunBottom = "b"
const bunTop = "B"
const patty = "p"
const tomato = "t"
const lettuce = "l"
const cheese = "C"
const bar = "1"
const done = "d"
const trash = "T"
const table = "2"
const fail = "f"

const melody = tune`
16000`





setLegend(
  [ cursor, bitmap`
................
................
.......0........
.......00.......
.......010......
........010.....
........0L10....
.........0210...
........00L210..
.........0LL0...
........0LL0....
........010.....
.......010......
.......00.......
.......0........
................`],
  [bunBottom, bitmap `
................
................
................
................
0000000000000000
0966666999666990
0999999999999960
0999999999999990
0999999999999990
0999999999999990
0399999999999990
.03339999999990.
..000000000000..
................
................
................`],
  [bunTop, bitmap `
................
................
................
................
................
..000000000000..
.03999999999990.
0396999969969930
0999969999999690
0999999999699990
0969999969999990
0999969999996990
0333333399999990
0000000000000000
................
................`],
  [patty, bitmap `
................
................
................
................
.00000000000000.
0CCCCCCCCCCC2220
0CCC0000000CCC20
0CCCCCCCCCCCCCC0
0CC000000000CCC0
0CCCCCCCCCCCCCC0
09CCC00000CCCCC0
099CCCCCCCCCCC90
.00000000000000.
................
................
................`],
  [tomato, bitmap `
................
................
................
................
...3333333333...
.33333993399333.
3333333933933333
3399933333333333
3333393333399933
9333333939333333
.93339933933333.
...3333333333...
................
................
................
................`],
  [lettuce, bitmap `
................
................
................
................
................
..444444444444..
44444D4444.444D4
D44.4444D4444444
4.4444.444444.4D
4444D44444.4D44D
..444444444444..
................
................
................
................
................`],
  [cheese, bitmap `
................
................
................
................
................
................
................
................
.66666666666666.
6666666666666666
9666666666699666
.66..........96.
.9............6.
................
................
................`],
  [bar, bitmap `
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....
....1L0.0L1.....`],
  [done, bitmap `
................
.......666......
........9.......
.......666......
......66266.....
.....6666266....
.....6666626....
....666666666...
....666666266...
....966666666...
....966666666...
....999966666...
........0.......
..LLLLLL0000000.
..0000000000000.
................`],
  [trash, bitmap`
................
.......00.......
....00000000....
...0LLLL11L10...
...0000000000...
....04D44D40....
....04D44D40....
....04D44D40....
....04D44D40....
....04D44D40....
....04D44D40....
....04D44D40....
....04D44D40....
....04D44D40....
....00000000....
................`],
  [table, bitmap `
CCCCCC999999C9CC
00CCCCCCCCCCCCCC
..CC........9C..
...CC......9C...
..C00C.99.9CCC..
..C..0CCC90..C..
...C..0CC0..9...
...C9..CC..9C...
....C..9C..C....
....C9.9C.9C....
.....CC9CCC.....
....C0.9C.0C....
...C0..9C..0C...
..C....9C....C..
.CCCCCCCCCCCCCC.
..C9999999999C..`],
  [fail, bitmap `
................
.33..........93.
.C39........93C.
..C39......93C..
...C39....93C...
....333..333....
.....333333.....
......3923......
......3393......
.....333333.....
....933..C39....
...933....339...
..933......C39..
.33C........C33.
.CC..........C3.
................`]
)

setSolids([])

let level = 0
const levels = [
  map`
1...1......
1...1...cB.
1...1....l.
1...1....t.
1...1....C.
1...1....p.
1...1....b.
1...1......
1...1....d.
1.2.1.2..T.
1...1......`,
  map `
.....
.fff.
.....
.....
.....
.....`
]

setMap(levels[level])




const options = [-1,0,1,2,3,4,5,-1,6,7,-1] // -1 is no option
const spriteOptions = [bunTop,lettuce,tomato,cheese,patty,bunBottom,done,trash]
  
let yourBurger = []
let orderedBurger = [5,4,3,0]
let playerBurgerIndex = 0
let fails = 0
let score = 0

const burgerOrder = () => {
    //for(let i = 0; i < orderedBurger.length; i++){
    //orderedBurger[i] = -1
  //}
  for(let i = 0; i <= 8; i++){
    clearTile(2,8-i)
  }
  
  orderedBurger.length = 0
  orderedBurger[0] = 5
  addSprite(2,8, bunBottom)
  
  for(let i = 1; i < Math.floor(Math.random() * 5)+2; i++){
    orderedBurger[i] =  Math.floor(Math.random() * 4)+1;
    addSprite(2,8-i, spriteOptions[orderedBurger[i]])
  }

  addSprite(2,8-orderedBurger.length, bunTop)
  orderedBurger[orderedBurger.length] = 0
  
}

burgerOrder()

addText("0", { 
  x: 4,
  y: 1,
  color: color`7`
})

onInput("s", () => {
  if(getFirst(cursor).y<9)
  getFirst(cursor).y += 1
  if(getFirst(cursor).y==7)
     getFirst(cursor).y += 1
})
onInput("w", () => {
  if(getFirst(cursor).y>1)
  getFirst(cursor).y -= 1
  if(getFirst(cursor).y==7)
     getFirst(cursor).y -= 1
})

onInput("d", () => {
  yourBurger[playerBurgerIndex] = options[getFirst(cursor).y]
  
  if(yourBurger[playerBurgerIndex] < 6){//topping
    
    addSprite(6, 8-playerBurgerIndex, spriteOptions[yourBurger[playerBurgerIndex]])
    playerBurgerIndex++
    
  } else if (yourBurger[playerBurgerIndex]==6){//bell
    let same = true
    if(orderedBurger.length != playerBurgerIndex)
      same=false

    
    for(let i = 0; i < orderedBurger.length; i++){
      if(orderedBurger[i] != yourBurger[i])
        same = false
    }
    if(!same){
      console.log("wrong")
      fails++
      addSprite(4+fails, 0, fail)
      score -= 10
    }else {
      console.log("correct");
      score+=10
      burgerOrder()
    }
    
    for(let i = 0; i <= 7; i++){
      yourBurger[i] = 0;
      clearTile(6,8-i)
    }
    
    playerBurgerIndex=0
    
    if(score < 0)
      score = 0
    
    clearText()
    addText(score.toString(), { 
      x: 4,
      y: 1,
      color: color`7`
    })

    if(fails==3){
      clearText()
      setMap(map `.....
.fff.
.....
.....
.....
.....`)
      addText("You Failed", { 
        x: 5,
        y: 6,
        color: color`3`
      })
      addText("Score:", { 
        x: 5,
        y: 8,
        color: color`7`
      })
      addText(score.toString(), { 
        x: 12,
        y: 8,
        color: color`7`
      })
      
    }
    
  } else {//trash
    
    for(let i = 0; i < 8; i++){
      yourBurger[i] = 0;
      clearTile(6,8-i)
    }
    playerBurgerIndex=0
  }
  

})


afterInput(() => {
  
})


